<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Adver;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class AdverController extends Controller
{
    //get Admin/user 全部用户列表
    public function index(Request $request)
    {
        $adver = Adver::select()->get();
        return view('admin.adver.adver',compact('adver'));
    }
    //get Admin/adver/create 添加用户信息
    public function create()
    {
        return view('admin/adver/adveradd');
    }
    public function show(){}
    public function chan(Request $request)
    {
        $input = Input::except('_token');
        $input['ctime']=time();
        $res = Adver::create($input);
        if($res){
            $data = ['status'=>0,
                'msg'=>'图片添加成功',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'图片添加失败，请重试',
            ];
        }
        return $data;
    }
    //    相片排序
    public function changeOrder(Request $request)
    {
        $input = $request->all();
        $album = Adver::find($input['id']);
        $album->order = $input['order'];
        $res = $album->save();
        if($res){
            $data = ['status'=>0,
                'msg'=>'图片排序更新成功',

            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'图片排序更新失败，请重试',
            ];
        }
        return $data;
    }
    //get Admin/adver/{adver}/edit   编辑图片信息
    public function edit($id)
    {
        $adds = Adver::find($id);
        return view('admin/adver/adveredit',compact('adds'));

    }

    //图片修改
    public function changedit(Request $request)
    {
        $id = $request->id;
        $input = Input::except('_token');
        $input['ctime']=time();
        $res = Adver::where('id',$id)->update($input);
        if($res){
            $data = ['status'=>0,
                'msg'=>'广告内容更新成功',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'广告内容更新失败，请重试',
            ];
        }
        return $data;

    }
    //delete Admin/banner/{banner}  删除用户信息
    public function destroy($id)
    {
        $count = count(Adver::select()->get()->toarray());
        if($count<=5){
            $data = [
                'status'=>1,
                'msg'=>'广告不能少于五个!',
            ];
            return $data;
        }
        $res = Adver::where('id',$id)->select()->get();
        $res = $res[0]->path;
        $str = str_replace('/','\\',$res);
        $res = Adver::where('id',$id)->delete();
        if($res){
            @unlink(public_path().$str);
            $data = [
                'status'=>0,
                'msg'=>'广告删除成功!',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'广告删除失败!',
            ];
        }
        return $data;
    }


    public function cs(){
       return view('admin/adver/adverweb');
    }
}
